let app = getApp()
const bmap = require('../libs/bmap-wx.min.js')

let config = {
    host: 'https://app.zhijianjiaxiang.com',
    // host: 'http://test.zhijianjiaxiang.com',
    ak: 'iTE4p9UaYtzXw9GjROfi37ZywNpymzAx'
}

function _get(url, data, doSuccess, doFail, doComplete) {
    wx.showLoading({
        title: '正在加载',
        mask: true
    })
    wx.request({
        url: config.host + url,
        data: data,
        method: 'GET', 
        header: {
            'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
            wx.hideLoading()
            if (typeof doSuccess == "function") {
                doSuccess(res)
            }
        },
        fail: function() {
            wx.hideLoading()
            if (typeof doFail == "function") {
                doFail()
            } else {
                wx.showToast({
                    title: '请求出错',
                    icon: 'none'
                })
            }
        },
        complete: function() {
            if (typeof doComplete == "function") {
                doComplete()
            }
        }
    })
}

function _post(url, data, doSuccess, doFail, doComplete) {
    wx.showLoading({
        title: '正在加载',
        mask: true
    })
    wx.request({
        url: config.host + url,
        data: data,
        method: 'POST',
        header: {
            'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
            wx.hideLoading()
            if (typeof doSuccess == "function") {
                doSuccess(res);
            }
        },
        fail: function(err) {
            wx.hideLoading()
            if (typeof doFail == "function") {
                doFail(err);
            } else {
                wx.showToast({
                    title: '请求出错',
                    icon: 'none'
                })
            }
        },
        complete: function(compl) {
            if (typeof doComplete == "function") {
                doComplete(compl);
            }
        }
    })
}

function _headerPost(url, data, doSuccess, doFail, doComplete) {
    wx.showLoading({
        title: '正在加载',
        mask: true
    })
    // console.log(app.globalData)
    wx.request({
        url: config.host + url,
        data: data,
        header: {
            'Authorization': app.globalData.token,
            'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        success: res => {
            wx.hideLoading()
            // 如果登录失效,去登录
            if (res.data.code == '-100') {
                wx.navigateTo({
                    url: '/pages/public/login/login'
                })
            } else if (typeof doSuccess == "function") {
                doSuccess(res);
            }
        },
        fail: function(err) {
            wx.hideLoading()
            if (typeof doFail == "function") {
                doFail(err);
            } else {
                wx.showToast({
                    title: '请求出错',
                    icon: 'none'
                })
            }
        },
        complete: function(compl) {
            if (typeof doComplete == "function") {
                doComplete(compl);
            }
        }
    });
}


function _headerGet(url, data, doSuccess, doFail, doComplete) {
    wx.showLoading({
        title: '正在加载',
        mask: true
    })
    // console.log(app.globalData)
    wx.request({
        url: config.host + url,
        data: data,
        header: {
            'Authorization': app.globalData.token,
            'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'GET',
        success: res => {
            wx.hideLoading()
            // 如果登录失效,去登录
            if (res.data.code == '-100') {
                wx.navigateTo({
                    url: '/pages/public/login/login'
                })
            } else if (typeof doSuccess == "function") {
                doSuccess(res);
            }
        },
        fail: function(err) {
            wx.hideLoading()
            if (typeof doFail == "function") {
                doFail(err);
            } else {
                wx.showToast({
                    title: '请求出错',
                    icon: 'none'
                })
            }
        },
        complete: function(compl) {
            if (typeof doComplete == "function") {
                doComplete(compl);
            }
        }
    })
}


function getLocation(doSuccess, doFail) { //获取地理位置
    wx.showLoading({
        title: '正在加载',
    })
    let BMap = new bmap.BMapWX({
        ak: config.ak
    })
    BMap.regeocoding({
        success: res => {
            doSuccess(res)
            wx.hideLoading()
        },
        fail: err => {
            doFail(err)
            wx.hideLoading()
        }
    })
}

module.exports = {
    config: config,
    _get: _get,
    _post: _post,
    _headerPost: _headerPost,
    _headerGet: _headerGet,
    getLocation: getLocation
};